<?php

namespace App\Http\Controllers\admin;

use App\Http\Controllers\Controller;
use App\Models\LaborTeam;
use App\Models\Project;
use App\Models\ProjectTeam;
use App\Services\ProjectService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class ProjectController extends Controller
{
    protected $ProjectService;
    public function __construct(ProjectService $ProjectService)
    {
        $this->ProjectService = $ProjectService;
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $title = $request->title;
        $pageSize = $request->pageSize ?: 10;
        $page = $request->page ?: 1;
        $page = $page * $pageSize - $pageSize;

        $sql = $this->ProjectService->get($title);
        $sql->where('company_id',$request->company_id);
        $total = $sql->count();
        $data = $sql->skip($page)->take($pageSize)->orderBy('created_at','desc')->get();
        return response()->json(['code' => 200, 'message' => '操作成功','page'=>$page,'pageSize'=>$pageSize,'total'=>$total,'data'=>$data]);
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function all(Request $request)
    {
        $title = $request->title;
        $sql = $this->ProjectService->get($title);
        $sql->where('company_id',$request->company_id)->select('id','name');
        $data = $sql->orderBy('created_at','desc')->get();
        return response()->json(['code' => 200, 'message' => '操作成功','data'=>$data]);
    }


    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function auth(Request $request)
    {
        $sql = $this->ProjectService->get('');
        $sql->select('id','name','number')->where('company_id',$request->company_id);
        $data = $sql->orderBy('created_at','desc')->get();
        return response()->json(['code' => 200, 'message' => '操作成功','data'=>$data]);
    }

    public function show(Request $request)
    {
        $data = Project::find($request->id);
        if($data->start_date){
            $data->start_date = strtotime($data->start_date) * 1000;
        }
        if($data->end_date){
            $data->end_date = strtotime($data->end_date) * 1000;
        }
        $data->teams;
        $teamids = [];
        $teams = [];
        foreach ($data->teams as $k => $v) {
            $arr = [];
            if($v->team){
                $teamids[] = $v->id;
                $arr['id'] = $v->team_id;
                $arr['name'] = $v->team['name'];
                $arr['director'] = $v->team['director'];
                $arr['contact_mobile'] = $v->team['contact_mobile'];
                $arr['type'] = $v->team['type'];
                $teams[] = $arr;
            }
        }
        $data->teamids = $teamids;

        $resources = $data->resources;
        $filesUrl = [];
        foreach ($resources as $k => $v) {
            $arr = [];
            $arr['id'] = $v->id;
            $arr['name'] = $v->resource->name;
            $arr['url'] = $v->resource->url;
            $filesUrl[] = $arr;
        }
        $data->filesUrl = $filesUrl;
        return response()->json(['code' => 200, 'message' => '操作成功','data'=>$data,'teams'=>$teams]);
    }


    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $POSTONLY = Project::POSTONLY;
        $input = $request->only($POSTONLY);
        $teamids = $request->teamids ? json_decode($request->teamids) : '';
        $data = $this->ProjectService->insert($input,$teamids);
        return response()->json(['code' => 200, 'message' => '操作成功','data'=>$data]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request)
    {
        $POSTONLY = Project::POSTONLY;
        $input = $request->only($POSTONLY);
        $input['id'] = $request->id;
        $teamids = $request->teamids ? json_decode($request->teamids) : '';
        $data = $this->ProjectService->update($input,$teamids);
        return response()->json(['code' => 200, 'message' => '操作成功','data'=>$data]);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function delete($id)
    {
        if(!$id){
            return response()->json(['code' => 404, 'message' => '请选择要操作的记录']);
        }
        $data = Project::find($id);
        $data->delete();
        return response()->json(['code' => 200, 'message' => '操作成功']);
    }


    public function teams(Request $request)
    {
        $data = ProjectTeam::with('team')->where('project_id',$request->id)->get();
        foreach ($data as $k => $v) {
            if($v->team){
                $teamids[] = $v->id;
                $data[$k]['name'] = $v->team['name'];
                $data[$k]['director'] = $v->team['director'];
                $data[$k]['contact_mobile'] = $v->team['contact_mobile'];
                $data[$k]['type'] = $v->team['type'];
            }
        }
        return response()->json(['code' => 200, 'message' => '操作成功','data'=>$data]);
    }

    public function teamInfo(Request $request)
    {
        $data = ProjectTeam::where('id',$request->id)->first();
        if($data->in_time){
            $data->in_time = strtotime($data->in_time) * 1000;
        }
        if($data->out_time){
            $data->out_time = strtotime($data->out_time) * 1000;
        }
        $team = LaborTeam::where('id',$data->team_id)->first();
        return response()->json(['code' => 200, 'message' => '操作成功','data'=>$data,'team'=>$team]);
    }

     /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function storeTeam(Request $request)
    {
        $POSTONLY = ProjectTeam::POSTONLY;
        $input = $request->only($POSTONLY);
        $input['admin_id'] = Auth::user()->id;
        $input['company_id'] = $request->company_id;
        $data = $this->ProjectService->insertTeam($input);
        return response()->json(['code' => 200, 'message' => '操作成功','data'=>$data]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function updateTeam(Request $request)
    {
        $POSTONLY = ProjectTeam::POSTONLY;
        $input = $request->only($POSTONLY);
        $input['admin_id'] = Auth::user()->id;
        $input['company_id'] = $request->company_id;
        $input['id'] = $request->id;
        $data = $this->ProjectService->updateTeam($input);
        return response()->json(['code' => 200, 'message' => '操作成功','data'=>$data]);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function deleteTeam($id)
    {
        if(!$id){
            return response()->json(['code' => 404, 'message' => '请选择要操作的记录']);
        }
        $data = ProjectTeam::find($id);
        $data->delete();
        return response()->json(['code' => 200, 'message' => '操作成功']);
    }



}
